package com.walle.ak47.commons.jaxrs;

import java.util.Locale;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.ext.ExceptionMapper;

import com.walle.ak47.commons.jaxrs.constant.ServerCode;

public class ServerExceptionMapper implements ExceptionMapper<Exception> {

	@Override
	public Response toResponse(Exception ex) {

		String reason = null;
		ResponseBuilder rb = Response.status(Response.Status.OK);
		rb.type("application/json;charset=UTF-8");
		if (ex.getMessage() == null)
			reason = "please check entry content, entry content must meet requirements protocol definition";
		else
			reason = ex.getMessage();
		BaseResp data = new BaseResp();
		data.setStatus(ServerCode.OTHER_ERROR.code());
		data.setReason(reason);
		rb.entity(data);
		rb.language(Locale.SIMPLIFIED_CHINESE);
		Response r = rb.build();
		return r;
	}
}
